﻿/******************************************************************
 *                  Dipan.SanGuo City Utils                       *
 *                                                                *
 * File Name: Dipan.SanGuo.City.js                                *
 * Written by: yangc (sheepchang@163.com)                         *
 * Important: to use this script don't                            *
 * remove these comments                                          *
 * Version 1.0 (MSIE 6.0 above,Firefox2.0,Netscape.)              *
 * Created Date: 2008-06-11                                       *
 ******************************************************************/

/*城池*/
Dipan.SanGuo.City =
{
    /**
     * 城池初始化

     */
    Initialize: function()
    {
        Dipan.SanGuo.City.GetBuilding();//刷建筑

        Dipan.SanGuo.City.GetCityInfo();//刷城市基本信息

        Dipan.SanGuo.City.GetInfoResource();//刷资源

        //Dipan.SanGuo.Common.CityList(); //刷城市列表

    },
    
    /**
     * 城池页面模板
     */
    Template:
    {
        BuildImages:["{for i in infos}",
                    "{if i[1] == 0}",
                    "",
                    "{elseif i[1] == 1}",
                    "<div class=\"build${i[0]}\" id=\"build${i[0]}\"><img src=\"${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/bulid/${i[1]}_${Dipan.SanGuo.City.GetBuildLevel(i[3])}.gif\" /></div>",
                    "{elseif i[1] == 2}",
                    "<div class=\"build${i[0]}\" id=\"build${i[0]}\"></div>",
                    "{elseif i[1] > 0 && i[1] < 22}",
                    "<div class=\"build${i[0]}\" id=\"build${i[0]}\"><img src=\"${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/bulid/${i[1]}_${Dipan.SanGuo.City.GetBuildLevel(i[3])}.gif\" /></div>",
                    "{elseif i[1] > 21}",
                    "",
                    "{/if}",
                    "{/for}"],
        
        BuildAreas:["{for i in infos}",
                    '<area shape=\"poly\" onmouseover=\"Dipan.SanGuo.City.ShowBuildTips(${i[1]},${i[3]},${i[4]},${i[0]});\" onmouseout=\"Dipan.SanGuo.City.HideBuildTips();\" id=\"build${i[0]}\" coords=\"${Dipan.SanGuo.City.GetBuildAreas(i[0])}\" href=\"#\" onclick=\"Dipan.SanGuo.City.Build(${i[0]}, ${i[1]});return false;\" onfocus=\"this.blur()\" />',
                    "{/for}",
                    ],
                    
        CityInfo:['<div class="n info_att white_12b" onmouseover="Dipan.SanGuo.City.ShowItem(\'民心：${morale[0]}<br />民怨：${morale[2]}<br />趋势：{if morale[1]==0}稳定{elseif morale[1]==1}上升{elseif morale[1]==2}下降{/if}\')" onmouseout="Dipan.SanGuo.City.HideItem()">',
                '<font class="black_12b">民心/民怨：</font><span title="民心">${morale[0]}</span> / <span title="民怨" class="orange_12b">${morale[2]}</span> / <span title="变化趋势" class="green_12b">{if morale[1]==0}稳定{elseif morale[1]==1}<font color="green">上升</font>{elseif morale[1]==2}<font color="#FCAA06">下降</font>{/if}</span>',
                '</div>',
                '<div class="n info_att white_12b" onmouseover="Dipan.SanGuo.City.ShowItem(\'税收：${tax}%<br />每小时铜钱收入：${money[1]}\')" onmouseout="Dipan.SanGuo.City.HideItem()">',
                '<font class="black_12b">税收：</font><span title="税收">${tax}%</span></div>',
                '<div class="n info_att_w white_12b" onmouseover="Dipan.SanGuo.City.ShowItem(\'城内铜钱存量：${money[0]}<br />每小时铜钱收入：${money[1]}\')" onmouseout="Dipan.SanGuo.City.HideItem()">',
                '<font class="black_12b">铜钱：</font><span title="城内铜钱存量" id="current_monkey">${money[0]}</span> / <span title="每小时铜钱收入" class="{if money[1]>0}green_12b{else}brown_12b{/if}">${money[1]}</span>',
                '</div>',
                '<div class="n info_att_w white_12b" onmouseover="Dipan.SanGuo.City.ShowItem(\'空闲人口：${population[4]}<br />',
                '劳动人口：${population[2]}<br />军事人口：${population[3]}<br />额外人口：${population[5]}<br />当前居民：${population[0]}<br />人口上限：${population[1]}<br />变化趋势：{if population[6]==0}稳定{elseif population[6]==1}上升{elseif population[6]==2}下降{/if}{if population[7]*1>0}<br />异常因素：{if (population[7]&0x1)>0}缺粮 {elseif (population[7]&0x2)>0}缺钱 {elseif (population[7]&0x4)>0}粮产不足 {elseif (population[7]&0x8)>0}民心下降 {/if}{/if}\')" onmouseout="Dipan.SanGuo.City.HideItem()">',
                '<font class="black_12b">人口：</font><span title="空闲人口">${population[4]}</span> / <span title="当前居民人口数" class="yellow_12b">${population[0]}</span> / ',
                '<span title="人口上限" class="black_12b">${population[1]}</span> / ',
                '<span title="变化趋势" class="orange_12b">{if population[6]==0}稳定{elseif population[6]==1}上升{elseif population[6]==2}下降{/if}</span>',
                '</div>'],
                
         Resource:['<div class="resource">',
'<ul>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'木材产量：${wood[1]}  ${Dipan.SanGuo.City.toChinese(wood[1])}<br />木材存量：${wood[0]}  ${Dipan.SanGuo.City.toChinese(wood[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==wood[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-wood[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-wood[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-wood[0]*1,wood[1],wood[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources01.gif" title="木材" />',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==wood[0]*1}orange_12b{elseif max_storage*0.8<=wood[0]*1}yellow3{else}yellow_8{/if}" id="current_wood">${wood[0]}</span></div>',
'<div class="{if wood[1]<=0}red{else}bright yellow8{/if}"><span id="output_wood">${wood[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'铁矿产量：${iron[1]}  ${Dipan.SanGuo.City.toChinese(iron[1])}<br />铁矿存量：${iron[0]}  ${Dipan.SanGuo.City.toChinese(iron[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==iron[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-iron[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-iron[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-iron[0]*1,iron[1],iron[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources02.gif" title="铁矿" />',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==iron[0]*1}orange_12b{elseif max_storage*0.8<=iron[0]*1}yellow3{else}yellow_8{/if}" id="current_iron">${iron[0]}</span></div>',
'<div class="{if iron[1]<=0}red{else}bright yellow8{/if}"><span id="output_iron">${iron[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'石料产量：${stone[1]}  ${Dipan.SanGuo.City.toChinese(stone[1])}<br />石料存量：${stone[0]}  ${Dipan.SanGuo.City.toChinese(stone[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==stone[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-stone[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-stone[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-stone[0]*1,stone[1],stone[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources03.gif" title="石料"/>',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==stone[0]*1}orange_12b{elseif max_storage*0.8<=stone[0]*1}yellow3{else}yellow_8{/if}" id="current_stone">${stone[0]}</span></div>',
'<div class="{if stone[1]<=0}red{else}bright yellow8{/if}"><span id="output_stone">${stone[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'粮食消耗：<span class=red2>${food[2]}</span>  ${Dipan.SanGuo.City.toChinese(food[2])}<br />粮食产量：${food[1]}  ${Dipan.SanGuo.City.toChinese(food[1])}<br />粮食存量：${food[0]}  ${Dipan.SanGuo.City.toChinese(food[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==food[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-food[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-food[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-food[0]*1,food[1],food[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources04.gif" title="粮食"/>',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==food[0]*1}orange_12b{elseif max_storage*0.8<=food[0]*1}yellow3{else}yellow_8{/if}" id="current_food">${food[0]}</span></div>',
'<div class="{if food[1]<=0}red{else}bright yellow8{/if}"><span id="output_food">${food[1]}</span>/h</div>',
'</b>',
'</li>',
'</ul>',
'</div>'],

        BuildTips:['<div class="top">',
'<span></span></div>',
'<div class="s_cont">',
'<div class="main2">',
'<div class="det_v yellow4 font12"><strong class="yellow2">${name}</strong><br />',
'${title}</div>',
'<div class="clear">',
'</div>',
'</div>',
'</div>',
'<div class="bottom">',
'<span></span></div>'],

        Troops:['<div class="troops_title">',
'<ul>',
'<li>城池名</li>',
'<li>将领</li>',
'<li>步兵</li>',
'<li>骑兵</li>',
'<li>弓箭兵</li>',
'<li>投石车</li>',
'</ul>',
'</div>',
'{if infos.length > 0}',
'<marquee onmouseover="this.stop()" onmouseout="this.start()" scrollAmount="1" direction="up" width="465" height="27">',
'<div class="troops_cont">',
'<table bgcolor="#9e8f6d" width="465" border="0" cellpadding="2" cellspacing="1" class="page_table_bg " height="27">',
'{for i in infos}',
'<tr class="tr">',
'<td width="71" align="center">${$("__select___item").innerHTML}</td>',
'<td width="71" align="center">${i[1]}</td>',
'<td width="71" align="center">${i[2]}</td>',
'<td width="71" align="center">${i[3]}</td>',
'<td width="71" align="center">${i[4]}</td>',
'<td width="79" align="center">${i[5]}</td>',
'</tr>',
'{/for}',
'</table>',
'</div>',
'</marquee>',
'{/if}'],

        Defend:['<div class="defence">',
'<ul>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/icon_win.gif" /></p>',
'<b>',
'<div class="a pink_12">城墙</div>',
'<div class="b white">${wall_level}级</div>',
'</b>',
'</li>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/defence01.gif" /></p>',
'<b>',
'<div class="a pink_12">箭塔</div>',
'<div class="b white">${arrow_tower}</div>',
'</b>',
'</li>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/defence04.gif" /></p>',
'<b>',
'<div class="a pink_12">拒马</div>',
'<div class="b white">${anti_horse}</div>',
'</b>',
'</li>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/defence05.gif" /></p>',
'<b>',
'<div class="a pink_12">陷阱</div>',
'<div class="b white">${trap}</div>',
'</b>',
'</li>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/defence02.gif" /></p>',
'<b>',
'<div class="a pink_12">礌石</div>',
'<div class="b white">${fall_stone}</div>',
'</b>',
'</li>',
'<li>',
'<p><img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/defence03.gif" /></p>',
'<b>',
'<div class="a pink_12">滚木</div>',
'<div class="b white">${roll_wood}</div>',
'</b>',
'</li>',
'</ul>',
'</div>'
]
    },
    DivItemDetail:['<div class="top"><span></span></div>',
                        '<div class="s_cont">',
                        '<div class="main2">',
                        '<div class="det yellow4 font12" id="index_show_text"></div>',
                        '<div class="clear"></div>',
                        '</div>',
                        '</div>',
                        '<div class="bottom"><span></span></div>'],

/**
*   还有多少小时满仓
*  1. 还差多少满仓
*  2. 产量
*  3. 存量
*/                      
timeto:function(num,cnum,cun)
{
    var re="";
    cnum=cnum*1;
    if(cnum==0)
        return "";//"<br />产量为零不可能满仓";
    if(cun==0&&cun<=0)
        return "";
    if(cnum<0)
        return "<br />"+this.timetoday(parseInt(cun*-1/cnum))+" 后仓库存量为零";
    if(cnum>0)
       return "<br />还差 "+this.timetoday(parseInt(num/cnum))+" 满仓";
},
timetoday:function(num)
{
    if(num<24)
        return num+ " 小时";
    if(num==24)
        return "1 天";
    var day=parseInt(num/24);
    var hour=num%24;
    if(day>0)
        return day+" 天零 "+hour +" 小时";
},
toChinese: function(num) 
{
    if(num==0)
        return ;
  var re = "";
  var fushu=false;
  var stri=num.toString();
  if(num.toString().substring(0,1)=="-")
  {
    fushu=true;
    num=num*-1;
    stri=num.toString();
  }
  if(stri.length<5)
    return "";
  if(stri.length>=9)//亿

    stri=stri.substring(0,stri.length-8)+"亿" + stri.substring(stri.length-8,stri.length);
    
  if(stri.length>=5)//万

  {
    if(stri.substring(stri.length-4,stri.length)*1==0)
        stri=stri.substring(0,stri.length-4)+"万";
    else
        stri=stri.substring(0,stri.length-4)+"万" + stri.substring(stri.length-4,stri.length);
  }
  if(fushu)
    re="负"+stri;
  else
    re=stri;
  return "(<span class=yellow_12>"+re +"</span>)"; 
//  if(!/^\d*(\.\d*)?$/.test(num)){return "Number is wrong!";} 

//  //var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
//  //var AA = new Array("0","1","2","3","4","5","6","7","8","9"); 
//  var AA = new Array("零","一","二","三","四","五","六","七","八","九"); 
//  var BB = new Array("","十","百","千","万","亿","点","");  
//  var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0;

//  for(var i=a[0].length-1; i>=0; i--) 
//  { 
//    switch(k) 
//    { 
//      case 0 : re = BB[7] + re; break; 
//      case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0])) 
//            re = BB[4] + re; break; 
//      case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break; 
//    } 
//    if(k%4 == 2 && a[0].charAt(i+2) != 0 && a[0].charAt(i+1) == 0) re = AA[0] + re; 
//    if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++; 
//  } 
//  
//  if(a.length>1) //加上小数部分(如果有小数部分) 
//  { 
//    re += BB[6]; 
//    for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)]; 
//  }
  
},

    /**
     * 显示信息浮层
     * @param {id} DOM ID
     */
    ShowItem: function(txt,yn)
    {
        var o = $("index_div");
        if(yn==1)o=$("DivItem");
        if (!o&&yn==null)
        {
            o = document.createElement('DIV');
            o.setAttribute("id", "index_div");
            document.body.appendChild(o);
            o.className="float_s";
            o.style.cssText = "position:absolute; z-index:1103755";
            o.innerHTML=this.DivItemDetail.join('');//'<div class="top"><span></span></div><div class="s_cont"><div id="index_show_text" style="padding:10px;color:#E0D5B6;"></div></div><div class="bottom"> <span></span></div>';
        }
        if(yn==1)
             $('show_text').innerHTML=txt;
        else
            $("index_show_text").innerHTML=txt;
        var cw = Math.max(document.body.clientWidth, document.documentElement.clientWidth);
        var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
        evt = (window.event) ? window.event : Dipan.SanGuo.City.ShowItem.caller.arguments[0];
        o.style.left = (evt.clientX + 320 > cw) ? (evt.clientX - 320) + "px" : (evt.clientX + 10) + "px";    
        if(yn==1)
            new Effect.Opacity('DivItem', {duration:0.0, from:0.7, to:1.0});
        else
            new Effect.Opacity('index_div', {duration:0.0, from:0.7, to:1.0});
        
        o.style.display = "";
        o.style.top = (evt.clientY - o.offsetHeight-20) + "px";
        //o.style.width='250px';
        //o.childNodes[1].style.width='240px';
        //o.childNodes[1].childNodes[0].style.width='100%';
        //o.childNodes[1].childNodes[0].childNodes[0].style.width='100%';
        //eval(fun);
    },
    
    /**
     * 隐藏信息浮层
     */
    HideItem: function()
    {
	    var o = $("index_div");
	    if(o)o.style.display = "none";
    },
    /**
    *   防

    */
    ShowDefanceItem: function(id)
    {
        var o = $("DivItem");
        if (!o)
        {
            o = document.createElement('DIV');
            o.setAttribute("id", "DivItem");
            document.body.appendChild(o);
            o.className="float_s";
            o.style.cssText = "position:absolute; z-index:1103755";
            o.innerHTML=this.DivItemDetail.join('');//'<div class="top"><span></span></div><div class="s_cont"><div id="index_show_text" style="padding:10px;color:#E0D5B6;"></div></div><div class="bottom"> <span></span></div>';
        }
        $('show_text').innerHTML=this.FindConfigDefence(id);
        var cw = Math.max(document.body.clientWidth, document.documentElement.clientWidth);
        var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
        evt = (window.event) ? window.event : Dipan.SanGuo.City.ShowDefanceItem.caller.arguments[0];
        o.style.left = (evt.clientX + 320 > cw) ? (evt.clientX - 320) + "px" : (evt.clientX + 10) + "px";
        new Effect.Opacity('DivItem', {duration:0.0, from:0.7, to:1.0});
        
        o.style.display = "";
        o.style.top = (evt.clientY - o.offsetHeight) + "px";
        
    },
    /**
    *   在配置中找出信息
    */
    FindConfigDefence:function(id)
    {
        var str="";
        var sid=(id.toString().substring(0,1)*1-1)*12;
        if(id.toString().substring(1,2)*1>0)
            sid+=6;
        if(id.toString().substring(0,1)*1>=5)
            sid=48;
        for(var i=sid;i<Dipan.SanGuo.Config.Defence.length;i++)
        {
            if(Dipan.SanGuo.Config.Defence[i][0]*1==id)
            {
                str+="攻击:"+Dipan.SanGuo.Config.Defence[i][3];
                str+="&nbsp;&nbsp;步防:"+Dipan.SanGuo.Config.Defence[i][4];
                str+="&nbsp;&nbsp;骑防:"+Dipan.SanGuo.Config.Defence[i][5];
                str+="&nbsp;&nbsp;弓防:"+Dipan.SanGuo.Config.Defence[i][6];
                str+="&nbsp;&nbsp;敏捷:"+Dipan.SanGuo.Config.Defence[i][7];
            }
        }
        return str;
    },
    
    BuildData:null,
    
    /**
     * 获取城池建筑
     */
    GetBuilding: function()
    {
        var RemoteData = new Dipan.SanGuo.Common.RemoteData();
        RemoteData.Post("/GateWay/City.ashx?id=5","");
        RemoteData.onComplete = function(resp)
        {
            var JsonData = eval('(' + resp + ')');
            
            if (JsonData.ret * 1 == 0)
            {
                try
                {
                    Dipan.SanGuo.City.BuildData = JsonData;
                    JsonData1 = Dipan.SanGuo.City.GetRealJsonData(JsonData);
                    Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.BuildImages, "BuildImages", JsonData1, true);
                    Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.BuildAreas, "BuildAreas", JsonData1, true);
                    
                    //20080909新增城池背景
                    if (JsonData.bg == "0")//默认
                    {
                        if (JsonData.infos.length == 0)
                            $("city_tent").className = "city bg_" + JsonData.size + "_" + 0;
                        else
                            $("city_tent").className = "city bg_" + JsonData.size + "_" + Dipan.SanGuo.City.GetWallLevel(JsonData.infos[0]);
                          
                    }
                    else if (JsonData.bg *1 >0 && JsonData.bg *1 < 7)
                    {
                        var bg = JsonData.bg*1 + 1;
                        if (JsonData.infos.length == 0)
                            $("city_tent").className = "city bg"+bg+"_" + JsonData.size + "_" + 0;
                        else
                            $("city_tent").className = "city bg"+bg+"_" + JsonData.size + "_" + Dipan.SanGuo.City.GetWallLevel(JsonData.infos[0]);
                    }
                    else
                    {
                        if (JsonData.infos.length == 0)
                            $("city_tent").className = "city bg_" + JsonData.size + "_" + 0;
                        else
                            $("city_tent").className = "city bg_" + JsonData.size + "_" + Dipan.SanGuo.City.GetWallLevel(JsonData.infos[0]);
                    }
                    
                    Dipan.SanGuo.Common.__TimeOut(JsonData);
                    Dipan.SanGuo.City.GetBuildingOther();
                    if(!$('info_all'))return;
                    if($('info_all').style.display=="")Dipan.SanGuo.Common.GetInfoAll();//更新升级聚合信息
                }
                catch (e)
                {
                    //alert(e.message);
                }
            }
            else
            {
                Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
            }
        }
    },
    
    /**
     * 其它页面获取建筑信息
     */
    GetBuildingOther: function()
    {
        if(parseInt(Dipan.SanGuo.Common.GetCookie('head_img'),10)!=-1)
        {
            this.GetAllyStatus2(parseInt(Dipan.SanGuo.Common.GetCookie('ally_id'),10),parseInt(Dipan.SanGuo.Common.GetCookie('head_img'),10));
            return;
        }
        var RemoteData = new Dipan.SanGuo.Common.RemoteData();
        RemoteData.Post("/GateWay/City.ashx?id=5","");
        RemoteData.onComplete = function(resp)
        {
            var JsonData = eval('(' + resp + ')');
            if (JsonData.ret * 1 == 0)
            {
                try
                {
                    Dipan.SanGuo.City.GetAllyStatus(JsonData);
                    
                }
                catch (e)
                {
                    //alert(e.message);
                }
            }
            else
            {
                Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
            }
        }
    },
    GetAllyStatus2:function(ally_id,head_img)
    {
        $("headimg_sm").src = Dipan.SanGuo.Common.StaticsServer + "/Statics/Images/headimg_sm/" + head_img + ".jpg";
        if(ally_id==0)
        {
            $("allystatus").href = "#";
            $("allystatus").onclick = function()
            {
                Dipan.SanGuo.City.Build(-1,5,0,1,0);
                return false;
            }
        }
    },
    /**
     * 获取联盟链接状态

     * @param {jsdata} 建筑信息
     */
//    GetAllyStatus: function(jsdata)
//    {
//        var RemoteData = new Dipan.SanGuo.Common.RemoteData();
//        RemoteData.Post("/GateWay/Common.ashx?id=23", "");
//        RemoteData.onComplete = function(resp)
//        {
//            var JsonData = eval ('(' + resp + ')');
//            if (JsonData.ret * 1 == 0)
//            {
//                $("headimg_sm").src = Dipan.SanGuo.Common.StaticsServer +  "/Statics/Images/headimg_sm/" + JsonData.head_img + ".jpg";
//                if (JsonData.ally_id == 0 && !Dipan.SanGuo.City.GetAllyBuild(jsdata))//无联盟无鸿胪寺

//                {
//                    $("allystatus").href = "#";
//                    $("allystatus").onclick = function()
//                    {
//                        alert("要加入或管理联盟，必须先建造一个鸿胪寺");
//                    }
//                }
//                else if (JsonData.ally_id > 0)//有联盟

//                {
//                    return;
//                }
//                else
//                {
//                    if (Dipan.SanGuo.City.GetAllyBuild(jsdata))//无联盟有鸿胪寺

//                    {
//                        $("allystatus").href = "#";
//                        $("allystatus").onclick = function()
//                        {
//                            var index = Dipan.SanGuo.City.GetAllyBuildIndex(jsdata);
//                            Dipan.SanGuo.City.Build(-1,5,0,1,0);
//                        }
//                    }
//                    else
//                    {
//                        alert("请先建造一个鸿胪寺");//无鸿胪寺
//                    }
//                }
//            }
//            else
//            {
//                //返回不成功后到统一方法处理错误信息
//                Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
//            }
//        }
//    },
    
    /**
     * 获取联盟建筑
     * @param {jsdata} 建筑信息
     */
    GetAllyBuild: function(data)
    {
        for (i = 0; i < data.infos.length; i++)
        {
            if (data.infos[i][1] == 5)
                return true;
        }
        
        return false;
    },
    
    /**
     * 获取联盟索引
     * @param {jsdata} 建筑信息
     */
    GetAllyBuildIndex: function(data)
    {
        for (i = 0; i < data.infos.length; i++)
        {
            if (data.infos[i][1] == 5)
                return i;
        }
    },
    
    /**
     * 获取城墙登级
     * @param {info} 城墙信息
     */
    GetWallLevel: function(info)
    {
        if (info[2] == "城墙")
        {
            if (info[3] >= 0 && info[3] <= 10)
                return "1";
            else if (info[3] > 10 && info[3] <= 20)
                return "2";
            else if (info[3] > 20)
                return "3";
        }
        else
            return "0";
    },
    
    /**
     * 获取建筑登级
     * @param {info} 建筑信息
     */
    GetBuildLevel: function(size)
    {
        if (size >= 0 && size <= 10)
            return "1";
        else if (size > 10 && size <= 20)
            return "2";
        else if (size > 20)
            return "3";
        else
            return "0";
    },
    
    /**
     * 获取真实建筑信息
     * @param {JsonData} 已经有的建筑信息
     */
    GetRealJsonData: function(JsonData)
    {
        var data;
        
        data = "{ret:"+JsonData.ret+",size:"+JsonData.size+",infos:[";
        
        if (JsonData.infos.length > 0)
        {
            for (i = 0; i < 31; i++)
            {
                var flag = false;
                for (n = 0; n < JsonData.infos.length; n++)
                {
                    if (i == JsonData.infos[n][0])
                    {
                        flag = true;
                        data += "["+JsonData.infos[n][0]+","+JsonData.infos[n][1]+",\'"+JsonData.infos[n][2]+"\',"+JsonData.infos[n][3]+","+JsonData.infos[n][4]+"],";
                    }
                }
                if (!flag)
                {
                    //一级城池缺少位置

                    if (JsonData.size == 1 && (i == 2 || i == 3 || i == 4 || i == 7 || i == 20 || i == 21 || i == 22 || i == 26 || i == 27 || i == 30))
                    {
                        continue;
                    }
                    //二级城池缺少位置
                    else if (JsonData.size == 2 && (i == 2 || i == 20 || i == 21 || i == 22 || i == 26 || i == 30))
                    {
                        continue;
                    }
                    //三级城池默认补空地

                    data += "["+i+",0,\"空地\",0,0],";
                }
            }
        }
        else
        {
            for (i = 0; i < 31; i++)
            {
                //一级城池缺少位置

                if (JsonData.size == 1 && (i == 2 || i == 3 || i == 4 || i == 7 || i == 20 || i == 21 || i == 22 || i == 26 || i == 27 || i == 30))
                {
                    continue;
                }
                //二级城池缺少位置
                else if (JsonData.size == 2 && (i == 2 || i == 20 || i == 21 || i == 22 || i == 26 || i == 30))
                {
                    continue;
                }
                //三级城池默认补空地

                data += "["+i+",0,\"空地\",0,0],";
            }
        }
        
        data += "],bg:" +JsonData.bg + "}";//alert(data);
        return eval('(' + data + ')');
    },
    
    /**
     * 获取城池基本信息
     */
    GetCityInfo: function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var RemoteData = new Dipan.SanGuo.Common.RemoteData()
            RemoteData.Post("/GateWay/City.ashx?id=6","");
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.CityInfo, "CityInfo", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 获取城池资源信息
     */
    GetInfoResource:function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var RemoteData = new Dipan.SanGuo.Common.RemoteData()
            RemoteData.Post("/GateWay/City.ashx?id=20","");
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.Resource, "cityinfo_tab", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 查询城池/营寨内军队信息

     */
    GetInfoTroops:function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var cityId=0;
            if (Dipan.SanGuo.Common.GetCookie("tid")*1>=0)
                cityId=Dipan.SanGuo.Common.GetCookie("cid");
            else
                cityId=Dipan.SanGuo.Common.GetCookie("tid");
            var nid = Dipan.SanGuo.Common.GetCookie("nid");
            if (nid*1 > 0)//是NPC城池
                cityId=Dipan.SanGuo.Common.GetCookie("nid");
                
            var RemoteData = new Dipan.SanGuo.Common.RemoteData();
            RemoteData.Post("/GateWay/Common.ashx?id=22","tid="+cityId);
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.Troops, "cityinfo_tab", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 查询城池/营寨内城防信息

     */
    GetInfoDefend:function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var RemoteData = new Dipan.SanGuo.Common.RemoteData();
            
            var cityId=0;
            if (Dipan.SanGuo.Common.GetCookie("tid")*1>=0)
                cityId=Dipan.SanGuo.Common.GetCookie("cid");
            else
                cityId=Dipan.SanGuo.Common.GetCookie("tid");
            var nid = Dipan.SanGuo.Common.GetCookie("nid");
            if (nid*1 > 0)//是NPC城池
                cityId=Dipan.SanGuo.Common.GetCookie("nid");

            RemoteData.Post("/GateWay/Common.ashx?id=17","tid="+cityId);
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.Defend, "cityinfo_tab", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 获取建筑位置
     * @param {pos} 建筑位置ID
     */
    GetBuildAreas: function(pos)
    {
        return Dipan.SanGuo.City.BuildAreas[pos];
    },
    
    /**
     * 建筑位置配置信息
     */
    BuildAreas:
    [
        [168,10,168,10,5,136,5,159,8,216,136,312,135,352,176,375,175,382,190,393,196,392,204,399,204,404,215,414,224,413,242,427,274,428,395,428,301,358,275,370,243,348,211,318,200,318,175,299,194,281,52,169,186,70,194,69,209,61,235,43,237,35,246,26,275,6,172,4],
        [703,3,664,38,705,79,741,56,744,36,743,13],
        [824,22,785,57,826,98,862,75,865,55,864,32],
        [286,7,247,42,288,83,324,60,327,40,326,17],
        [380,5,341,40,382,81,418,58,421,38,420,15],
        [620,23,581,58,622,99,658,76,661,56,660,33],
        [770,67,731,102,772,143,808,120,811,100,806,80],
        [332,50,293,85,334,126,370,103,373,83,365,64],
        [482,38,417,60,405,142,484,224,588,141,582,106,521,42],
        [721,144,682,179,723,220,759,197,762,177,754,158],
        [256,103,217,138,258,179,294,156,297,136,289,117],
        [123,126,84,161,125,202,161,179,164,159,156,140],
        [200,150,161,185,202,226,238,203,241,183,233,164],
        [322,168,283,203,324,244,360,221,363,201,355,182],
        [447,216,408,251,449,292,485,269,488,249,480,230],
        [636,166,597,201,638,242,674,219,677,199,669,180],
        [566,212,527,247,568,288,604,265,607,245,599,226],
        [693,207,654,242,695,283,731,260,734,240,726,221],
        [640,245,601,280,642,321,678,298,681,278,673,259],
        [591,285,552,320,593,361,629,338,632,318,624,299],
        [807,195,768,230,809,271,845,248,848,228,840,209],
        [774,255,735,290,776,331,812,308,815,288,807,269],
        [854,235,815,270,856,311,892,288,895,268,887,249],
        [247,223,208,258,249,299,285,276,288,256,280,237],
        [343,295,304,330,345,371,381,348,384,328,376,309],
        [473,285,434,320,475,361,511,338,514,318,506,299],
        [391,340,352,375,393,416,429,393,432,373,424,354],
        [514,345,475,380,516,421,552,398,555,378,547,359],
        [705,309,666,344,707,385,743,362,746,342,738,323],
        [654,351,615,386,656,427,692,404,695,384,687,365],
        [824,325,785,360,826,401,862,378,865,358,857,339]
    ],
    
    /**
     * 根据建筑唯一ID显示建筑浮层
     * @param {pid} 建筑位置ID
     * @param {gid} 建筑唯一ID
     */
    Build: function(pid,gid,id,tab,cityId)
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        if(!id)id=0;
        if(!tab)tab=1;
        if(!cityId)cityId=0;
        Loader.onsuccess = function()
        {
            switch (gid)
            {
                case 0 ://空地
                    Dipan.SanGuo.Build.Fun.emptyLand(pid,gid,id,tab,cityId);
                    break;
                case 1 ://官府
                    Dipan.SanGuo.Build.Fun.government(pid,gid,id,tab,cityId);
                    break;
                case 2 ://城墙
                    Dipan.SanGuo.Build.Fun.wall(pid,gid,id,tab,cityId);
                    break;
                case 3 ://民房
                    Dipan.SanGuo.Build.Fun.house(pid,gid,id,tab,cityId);
                    break;
                case 4 ://仓库
                    Dipan.SanGuo.Build.Fun.campwarehouse(pid,gid,id,tab,cityId);
                    break;
                case 5 ://鸿胪寺
                    Dipan.SanGuo.Build.Fun.diplomacy(pid,gid,id,tab,cityId);
                    break;
                case 6 ://大学殿
                    Dipan.SanGuo.Build.Fun.academy(pid,gid,id,tab,cityId);
                    break;
                case 7 ://客栈
                    Dipan.SanGuo.Build.Fun.inn(pid,gid,id,tab,cityId);
                    break;
                case 8 ://酒馆
                    Dipan.SanGuo.Build.Fun.pub(pid,gid,id,tab,cityId);
                    break;
                case 9 ://聚贤楼
                    Dipan.SanGuo.Build.Fun.official(pid,gid,id,tab,cityId);
                    break;
                case 10 ://点将台
                    Dipan.SanGuo.Build.Fun.general(pid,gid,id,tab,cityId);
                    break;
                case 11 ://市场
                    Dipan.SanGuo.Build.Fun.market(pid,gid,id,tab,cityId);
                    break;
                case 12 ://驿站
                    Dipan.SanGuo.Build.Fun.posthouse(pid,gid,id,tab,cityId);
                    break;
                case 13 ://兵器铺

                    Dipan.SanGuo.Build.Fun.weapon(pid,gid,id,tab,cityId)
                    break;
                case 14 ://防具铺

                    Dipan.SanGuo.Build.Fun.shield(pid,gid,id,tab,cityId);
                    break;
                case 15 ://车马间

                    Dipan.SanGuo.Build.Fun.traffic(pid,gid,id,tab,cityId);
                    break;
                case 16 ://校场
                    Dipan.SanGuo.Build.Fun.barracks(pid,gid,id,tab,cityId);
                    break;
                case 17 ://营寨栅栏
                    Dipan.SanGuo.Build.Fun.wall(pid,gid,id,tab,cityId);
                    break;
                case 18 ://中军大帐
                    Dipan.SanGuo.Build.Fun.headquarters(pid,gid,id,tab,cityId);
                    break;
                case 19 ://营寨校场
                    Dipan.SanGuo.Build.Fun.barracks(pid,gid,id,tab,cityId);
                    break;
                case 20 ://营寨驿站
                    Dipan.SanGuo.Build.Fun.posthouse(pid,gid,id,tab,cityId);
                    break;
                case 21 ://营寨仓库
                    Dipan.SanGuo.Build.Fun.campwarehouse(pid,gid,id,tab,cityId);
                    break;
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/Dipan.SanGuo.Config.js", true);
    },
    
    BuildingName: function(gid, level, status)
    {
        var buildname;
        switch (gid)
        {
            case 0 ://空地
                buildname = "空地";
                break;
            case 1 ://官府
                buildname = "官府";
                break;
            case 2 ://城墙
                buildname = "城墙";
                break;
            case 3 ://民房
                buildname = "民房";
                break;
            case 4 ://仓库
                buildname = "仓库";
                break;
            case 5 ://鸿胪寺

                buildname = "鸿胪寺";
                break;
            case 6 ://大学殿

                buildname = "大学殿";
                break;
            case 7 ://客栈
                buildname = "客栈";
                break;
            case 8 ://酒馆
                buildname = "酒馆";
                break;
            case 9 ://聚贤楼

                buildname = "聚贤楼";
                break;
            case 10 ://点将台

                buildname = "点将台";
                break;
            case 11 ://市场
                buildname = "市场";
                break;
            case 12 ://驿站
                buildname = "驿站";
                break;
            case 13 ://兵器铺

                buildname = "兵器铺";
                break;
            case 14 ://防具铺

                buildname = "防具铺";
                break;
            case 15 ://车马间

                buildname = "车马间";
                break;
            case 16 ://校场
                buildname = "校场";
                break;
            case 17 ://营寨栅栏
                buildname = "营寨栅栏";
                break;
            case 18 ://中军大帐
                buildname = "中军大帐";
                break;
            case 19 ://营寨校场
                buildname = "营寨校场";
                break;
            case 20 ://营寨驿站
                buildname = "营寨驿站";
                break;
            case 21 ://营寨仓库
                buildname = "营寨仓库";
                break;
            default :
                buildname = "空地";
                break;
        }
        
        switch (status)
        {
            case 0 ://正常
                {
                    if (gid != 0)
                    {
                        buildname += " " + level + " 级";
                    }
                }
                break;
            case 1 ://升级中

                buildname += " 正在从 " + level + " 级升到 " + (level+1) + " 级";
                break;
            case 2 ://拆除中

                buildname += " 正在从 " + level + " 级拆除到 " + (level-1) + " 级";
                break;
            case 3 ://顶级
                buildname += " " + level + " 级";
                break;
            default :
                buildname += " " + level + " 级";
                break;
        }
        
        return buildname;
    },
    
    ShowBuildTips: function(gid, level, status, pid)
    {
        var name = "";
        var title = "";
        
        if (pid == 0)//为城墙直接显示城墙信息

        {
            name = Dipan.SanGuo.Config.Buildings[2][0];
            title = Dipan.SanGuo.City.BuildingName(2,level,status);
        }
        else
        {
            name = Dipan.SanGuo.Config.Buildings[gid][0];
            title = Dipan.SanGuo.City.BuildingName(gid,level,status);
        }
        
        var data = {name:name,title:title};
        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.BuildTips, "BuildTips", data, true,-1);
        try
        {
            
        }
        catch (e)
        {
            //alert(e.message);
        }
        
        var o = $("BuildTips");
        if (o)
        {
            o.style.cssText = "position:absolute; z-index:1103755";
            var cw = Math.max(document.body.clientWidth, document.documentElement.clientWidth);
            var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
            evt = (window.event) ? window.event : Dipan.SanGuo.City.ShowBuildTips.caller.arguments[0];
            o.style.left = (evt.clientX + 260 > cw) ? (evt.clientX - 260) + "px" : (evt.clientX - 2) + "px";    
            o.style.top = (evt.clientY + 20 + st) + "px";
        }
    },
    
    /**
     * 隐藏信息浮层
     */
    HideBuildTips: function()
    {
	    var o = $("BuildTips");
	    if(o)o.style.display = "none";
    }
};